local GM = GM or gmod.GetGamemode()

local Tex_Corner8 = Tex_Corner8 or surface.GetTextureID("gui/corner8")
local Tex_Inv = Tex_Inv or surface.GetTextureID("gui/sniper_corner")


GM.inventory = {}
GM.ingredients = {}
local inv = GM.inventory

inv.openInventory = function()
	if type(inv.menuInventory) == "table" then
		return
	end
	
	local self = gmod.GetGamemode()
	local menu = {}
	menu.main = self.genwhitegui("Inventory")
		menu.main:SetName("menu.main")
		menu.main:SetPos(math.max(0,ScrW()*.5-512),ScrH()*.5-256)
		menu.main:SetPos(96,ScrH()-100-(492+35))
		menu.main:SetSize(276+10+32,492+35)
		menu.main:SetVisible(true)
		menu.main.OnKeyCodePressed = function(self,char)
			//Msg("KEYCODE: ",char,"\n")
			local self = gmod.GetGamemode()
			local out
			if char >= 2 and char <= 10 then
				out = char - 1
			elseif char == 1 or char == 27 then //0 or q
				out = 0
			end
			
			if out then //Numeric
				if self.inventory.menuInventory and self.inventory.menuInventory.main:IsVisible() and self.inventory.hover then
					//Add item to slot inventory
					self.invsel.items[out] = self.inventory.hover
					if out == 0 then
						do //TODO: Centralize me
							self.invsel.select = 0
							self.invsound()
							self.invsel.last = UnPredictedCurTime()
						end
						
						if char == 27 then //q
							self.inventory.closeInventory()
							self.lootventory.closeAll()
							self.slotinvquickclose()
						end
					end
				end
			elseif char == 13 or char == 32 then //c or v
				self.inventory.closeInventory()
				self.lootventory.closeAll()
			elseif char == 15 then //e
				if self.inventory.menuInventory and self.inventory.menuInventory.main:IsVisible() and self.inventory.hover then
					RunConsoleCommand("wsuse",self.inventory.hover.class)
				end
			end
		end
	menu.panel = vgui.Create("gmInventory",menu.main,"gmInventory")
		menu.panel:SetPos(5,30)
		menu.panel:SetSize(276+32,492)
		menu.panel:SetVisible(true)
	inv.menuInventory = menu
	
	local mx,my = menu.main:GetPos()
	local mw,mh = menu.main:GetSize()
	mx = mx + mw*.5
	my = my + mh*.5
	gui.SetMousePos(mx,my)
	
	if UnPredictedCurTime() > self.invsel.last + self.invsel.fade + self.invsel.fadetime then
		surface.PlaySound("itemopen.wav")
		self.invsel.lastsound = UnPredictedCurTime()
	end
end

inv.menuInvPanel = {
	Init = function(self)
		local dat = self:GetParent():GetTable()
		dat.m = {
			x=0,
			y=0,
			c={},
			f=0,
		}
		dat.scroll = 0
		dat.tab = 1
		dat.tabscroll = {
			0,
			0,
		}
	end,
	Paint = function(self)
		local GM = gmod.GetGamemode()
		GM.invsel.last = UnPredictedCurTime()
		local dat = self:GetParent():GetTable()
		local m = dat.m
		
		//surface.SetDrawColor(255,255,255,255)
		//surface.DrawRect(0,0,self:GetWide(),self:GetTall())
		
		
		//Inventory box
		local ix = 0
		local iy = 20
		local iw = self:GetWide()-32
		local ih = self:GetTall()-128-20
		//Drawbox moved
		
		//Combine
		local cx = 0
		local cy = self:GetTall()-128+5
		local cw = self:GetWide()
		local ch = 128-5
		draw.RoundedBox(4,cx,cy,cw,ch,Color(0,0,0,100))
		
		//Tabs
		
		local alpha = dat.tab == 1 and 100 or 50
		local col = Color(0,0,0,alpha)
		if m.x >= 8 and m.x <= 78 and m.y >= 0 and m.y <= 20 then
			col = Color(0,0,255,alpha)
			if m.c[0] == 1 then
				m.c[0] = 2
				m.f = "tab1"
			elseif m.c[0] == 2 and m.f == "tab1" then
				col = Color(255,0,0,alpha)
			elseif m.c[0] == -1 and m.f == "tab1" then
				m.c[0] = 0
				if dat.tab ~= 1 then
					dat.tabscroll[dat.tab] = dat.scroll
					dat.tab = 1
					dat.scroll = dat.tabscroll[dat.tab] or 0
					
					self:OnCheckScroll()
				end
			end
		end
		surface.SetDrawColor(col.r,col.g,col.b,col.a)
		surface.DrawRect(8+4,0,70-8,4)
		surface.DrawRect(8,4,70,16)
		surface.SetTexture(Tex_Corner8)
		surface.DrawTexturedRectRotated(8+2,2,4,4,0)
		surface.DrawTexturedRectRotated(8+70-4+2,2,4,4,270)
		surface.SetTexture(Tex_Inv)
		surface.DrawTexturedRectRotated(8-4,20-4,8,8,180)
		surface.DrawTexturedRectRotated(8+4+70,20-4,8,8,90)
		draw.DrawText("Inventory","Trebuchet18",8+70/2,2,Color(255,255,255,255),1)
		
		local alpha = dat.tab == 2 and 100 or 50
		local col = Color(0,0,0,alpha)
		if m.x >= 80 and m.x <= 150 and m.y >= 0 and m.y <= 20 then
			col = Color(0,0,255,alpha)
			if m.c[0] == 1 then
				m.c[0] = 2
				m.f = "tab2"
			elseif m.c[0] == 2 and m.f == "tab2" then
				col = Color(255,0,0,alpha)
			elseif m.c[0] == -1 and m.f == "tab2" then
				m.c[0] = 0
				if dat.tab ~= 2 then
					dat.tabscroll[dat.tab] = dat.scroll
					dat.tab = 2
					dat.scroll = dat.tabscroll[dat.tab] or 0
					
					self:OnCheckScroll()
				end
			end
		end
		surface.SetDrawColor(col.r,col.g,col.b,col.a)
		surface.DrawRect(80+4,0,70-8,4)
		surface.DrawRect(80,4,70,16)
		surface.SetTexture(Tex_Corner8)
		surface.DrawTexturedRectRotated(80+2,2,4,4,0)
		surface.DrawTexturedRectRotated(80+70-4+2,2,4,4,270)
		surface.SetTexture(Tex_Inv)
		surface.DrawTexturedRectRotated(80-4,20-4,8,8,180)
		surface.DrawTexturedRectRotated(80+4+70,20-4,8,8,90)
		draw.DrawText("Recipes","Trebuchet18",80+70/2,2,Color(255,255,255,255),1)
		
		//Inventory
		local inventory = GM.inv
		GM.inventory.hover = nil
		
		local x = {
			p = 1,
			m = 0,
			s = 64,
			b = 4,
		}
		local y = {
			p = 1,
			m = 0,
			s = 64,
			b = 4,
		}
		//lawl, PMS
		
		x.m = math.floor(iw/(x.s+x.b))
		y.m = math.floor(ih/(y.s+y.b))
		
		//Calculate mouse position
		local mx, my
		if m.x > ix and m.x < ix+iw and m.y > iy and m.y < iy+ih then
			mx = m.x-ix-4
			my = m.y-iy-4
			
			local mrx = mx%(x.s+x.b)
			local mry = my%(y.s+y.b)
			
			if mrx <= x.s and mry <= y.s then
				mx = math.floor(mx/(x.s+x.b))+1
				my = math.floor(my/(y.s+y.b))+1
			end
		end
		local active = GM.invdrag and GM.invdrag.item and GM.invdrag.itemfrom ~= -1 and mx or false
		if mx then
			//GM.lootventory.active = -1
			GM.invdrag.itemto = -1
			GM.invdrag.itemtolabel = "Inventory"
		end
		//Inventory box draw
		draw.RoundedBox(4,ix,iy,iw,ih,(not active) and Color(0,0,0,100) or Color(0,0,255,100))
		
		local maxrows,rows
		
		if dat.tab == 1 then
			//Scrollbar
			maxrows = y.m
			rows = math.max(math.ceil(#inventory/x.m),maxrows)
			//Etc
			
			local desc = {
			}
			
			for k,v in pairs(inventory) do
				if x.p > x.m then
					x.p = 1
					y.p = y.p+1
				end
				if y.p > y.m then
					x.p = x.m+1
					y.p = y.p-1
					break
				end
				
				
				local px = x.b+ix+(x.p-1)*(x.s+x.b)
				local py = y.b+iy+(y.p-1)*(y.s+y.b)
				local pw = x.s
				local ph = y.s
				
				local col = Color(0,0,0,63)
				if x.p == mx and y.p == my then
					col = Color(0,0,255,63)
					
					desc.x = x.b+ix
					desc.y = py+y.s+y.b
					desc.w = iw-x.b*2
					desc.h = y.s+y.b+y.s
					desc.item = v.item
					
					GM.inventory.hover = v.item
					
					if m.c[0] == 1 then
						m.c[0] = 2
						m.f = {"invdrag",k}
						GM.invdrag.item = v.item
						GM.invdrag.itemfrom = -1
						GM.invdrag.itemfromlabel = "Inventory"
					end
				end
				
				surface.SetDrawColor(col.r,col.g,col.b,col.a)
				surface.DrawRect(px,py,pw,ph)
				
				v.item:onInvPaint(px,py,pw,ph)
				
				x.p = x.p+1
			end
			
			//Inventory description
			
			if desc.item then
				surface.SetDrawColor(0,0,0,230)
				surface.DrawRect(desc.x,desc.y,desc.w,desc.h)
				
				local item = desc.item
				draw.DrawText(item.label,"Trebuchet24",4+desc.x,desc.y,Color(255,255,255,230),0)
				draw.DrawText(string.format("%.2f",item.weight) .. " kg","Trebuchet24",desc.x+desc.w-4,desc.y+desc.h-24,Color(255,255,255,230),2)
				
				//Word wrap
				surface.SetFont("Trebuchet18")
				
				local dp = string.Explode(" ",item.description)
				local dout = dp[1]
				local curline = dout
				for k,v in pairs(dp) do
					if k ~= 1 then
						local sz = surface.GetTextSize(curline)
						local sza = surface.GetTextSize(" "..v)
						if sz+sza < desc.w then
							dout = dout .. " " .. v
							curline = curline .. " " .. v
						else
							dout = dout .. "\n" .. v
							curline = v
						end
					end
				end
				draw.DrawText(dout or "","Trebuchet18",4+desc.x,desc.y+24,Color(255,255,255,230),0)
			end
			
			
			//Combine
			
			local ing = GM.ingredients
			
			//5 Maximum
			
			//Calculate row
			local row = 0
			local iconsz = 16
			if m.x >= cx+8 and m.x <= cx+cw-8-(64+4) and m.y >= cy+8 and m.y <= cy+ch-8 then
				row = math.ceil((m.y-cy-6)/(iconsz+4))
			end
			
			if GM.invdrag then
				//TODO: Remove this tag on close
				GM.invdrag.hovercombine = nil
			end
			
			if row >= 1 and row <= 5 then
				local col = Color(0,0,0,100)
				
				if GM.invdrag.item then
					col = Color(0,255,0,100)
					GM.invdrag.hovercombine = row
				else
					if m.c[0] == 1 then
						m.c[0] = 2
						m.f = {"comrow",row}
					end
					if type(m.f) == "table" and m.f[1] == "comrow" and m.f[2] == row then
						col = Color(255,0,0,100)
						if m.c[0] == -1 then
							ing[row] = nil
						end
					end
				end
				
				draw.RoundedBox(4,cx+4+2,cy+8+(iconsz+4)*(row-1)-2,cw-12-(64+4),iconsz+2+2,col)
			end
			
			//cx,cy,cw,ch
			for k,v in pairs(ing) do
				local col = Color(0,0,0,100)
				local ypos = cy+8+(iconsz+4)*(k-1)
				
				/*
				if row == k then
					//draw.RoundedBox(4,cx+4+2,ypos-2,cw-12-(64+4),iconsz+2+2,col)
				end
				*/
				
				draw.RoundedBox(4,cx+8,ypos,iconsz,iconsz,col)
				
				surface.SetTexture(GM.items[v]:onGetIconTexID())
				surface.SetDrawColor(255,255,255,255)
				surface.DrawTexturedRect(cx+8,ypos,iconsz,iconsz)
				
				draw.DrawText(tostring(GM.items[v].label),"Trebuchet18",cx+8+iconsz+6,ypos-1,Color(255,255,255,230),0)
			end
			
			local pd = {
				x = self:GetWide()-5-64,
				y = self:GetTall()-5-20-5-20,
				w = 64,
				h = 20,
			}
			local col = Color(0,0,0,100)
			if m.x >= pd.x and m.x <= pd.x+pd.w and m.y >= pd.y and m.y <= pd.y+pd.h then
				col = Color(0,0,255,100)
				if m.c[0] == 1 then
					m.c[0] = 2
					m.f = "comcombine"
				elseif m.c[0] == 2 and m.f == "comcombine" then
					col = Color(255,0,0,100)
				elseif m.c[0] == -1 and m.f == "comcombine" then
					m.c[0] = 0
					//Actually sends the command
					
					local out = {
						"wsrecipe"
					}
					
					for k,v in pairs(GM.ingredients) do
						table.insert(out,v)
					end
					
					if #out > 1 then
						RunConsoleCommand(unpack(out))
					end
				end
			end
			draw.RoundedBox(4,pd.x,pd.y,pd.w,pd.h,col)
			draw.DrawText("Combine","Trebuchet18",self:GetWide()-5-32,self:GetTall()-5-20-5-20+2,Color(255,255,255,255),1)
			
			local pd = {
				x = self:GetWide()-5-64,
				y = self:GetTall()-5-20,
				w = 64,
				h = 20,
			}
			local col = Color(0,0,0,100)
			if m.x >= pd.x and m.x <= pd.x+pd.w and m.y >= pd.y and m.y <= pd.y+pd.h then
				col = Color(0,0,255,100)
				if m.c[0] == 1 then
					m.c[0] = 2
					m.f = "comreset"
				elseif m.c[0] == 2 and m.f == "comreset" then
					col = Color(255,0,0,100)
				elseif m.c[0] == -1 and m.f == "comreset" then
					m.c[0] = 0
					GM.ingredients = {}
				end
			end
			draw.RoundedBox(4,pd.x,pd.y,pd.w,pd.h,col)
			draw.DrawText("Reset","Trebuchet18",self:GetWide()-5-32,self:GetTall()-5-20+2,Color(255,255,255,255),1)
			
		else //Recipe list
			local list = GM.recipes
			
			//Scrollbar
			maxrows = math.floor((ih-8)/28)
			rows = math.max(table.Count(list),maxrows)
			//Etc
			
			//Calculate mouse hover
			local hover = 0
			if m.x >= ix+4 and m.x <= ix+iw-4 and m.y >= iy+4 and m.y <= iy+ih-4 then
				hover = math.ceil((m.y-24+2)/(24+4))
			end
			if hover > maxrows then
				hover = 0
			end
			
			local recipe
			
			local i = -dat.scroll
			for k,v in pairs(list) do
				i = i+1
				if i > 0 and i <= maxrows then
					local f = {
						x = ix+4,
						y = iy+4+(24+4)*(i-1),
						w = iw-8,
						h = 24,
					}
					local firstproduct
					for i,o in pairs(v.products) do
						firstproduct = GM.items[i]
						break
					end
					
					if firstproduct then
						local col = Color(0,0,0,63)
						if hover == i then
							col = Color(0,0,255,63)
							recipe = k
							
							if m.c[0] == 1 then
								m.c[0] = 2
								m.f = "recselect"
							elseif m.c[0] == 2 and m.f == "recselect" then
								col = Color(255,0,0,100)
							elseif m.c[0] == -1 and m.f == "recselect" then
								m.c[0] = 0
								//Actually sends the command
								RunConsoleCommand("wslistedrecipe",recipe)
							end
						end
						draw.RoundedBox(4,f.x,f.y,f.w,f.h,col)
						
						draw.RoundedBox(4,f.x+4,f.y+4,16,16,Color(0,0,0,100))
						surface.SetTexture(firstproduct:onGetIconTexID())
						surface.SetDrawColor(255,255,255,255)
						surface.DrawTexturedRect(f.x+4,f.y+4,16,16)
						
						draw.DrawText(k,"Trebuchet24",f.x+24+4,f.y,Color(255,255,255,255),0)
					end
				end
			end
			
			if recipe then
				draw.DrawText(recipe,"Trebuchet24",cx+4,cy,Color(255,255,255,255),0)
				local bx
				local recdat = GM.recipes[recipe]
				
				bx = cx
				draw.DrawText("Products","Trebuchet18",bx+4,cy+24,Color(255,255,255,255),0)
				local i = 0
				for k,v in pairs(recdat.products or {}) do
					local item = GM.items[k]
					
					if item then
						draw.RoundedBox(4,bx+4,cy+24+18+18*i,16,16,Color(0,0,0,100))
						surface.SetTexture(item:onGetIconTexID())
						surface.SetDrawColor(255,255,255,255)
						surface.DrawTexturedRect(bx+4,cy+24+18+18*i,16,16)
						
						local col = Color(255,255,255,255)
						if not (GM.inv[k] and GM.inv[k].quantity >= 1) then
							col = Color(0,255,0,255)
						end
						
						draw.DrawText(v.."x "..item.label,"Trebuchet18",bx+16+4+4,cy+24+18+18*i,col,0)
					else
						draw.DrawText("ERROR","Trebuchet18",bx+16+4+4,cy+24+18+18*i,Color(255,0,0,255),0)
					end
					i = i+1
				end
				
				bx = cx+(cw/3)
				draw.DrawText("Reagents","Trebuchet18",bx+4,cy+24,Color(255,255,255,255),0)
				local i = 0
				for k,v in pairs(recdat.reagents or {}) do
					local item = GM.items[k]
					
					if item then
						draw.RoundedBox(4,bx+4,cy+24+18+18*i,16,16,Color(0,0,0,100))
						surface.SetTexture(item:onGetIconTexID())
						surface.SetDrawColor(255,255,255,255)
						surface.DrawTexturedRect(bx+4,cy+24+18+18*i,16,16)
						
						local col = Color(255,255,255,255)
						if not (GM.inv[k] and GM.inv[k].quantity >= v) then
							col = Color(255,0,0,255)
						end
						
						draw.DrawText(v.."x "..item.label,"Trebuchet18",bx+16+4+4,cy+24+18+18*i,col,0)
					else
						draw.DrawText("ERROR","Trebuchet18",bx+16+4+4,cy+24+18+18*i,Color(255,0,0,255),0)
					end
					i = i+1
				end
				
				bx = cx+(cw/3*2)
				draw.DrawText("Tools","Trebuchet18",bx+4,cy+24,Color(255,255,255,255),0)
				local i = 0
				for k,v in pairs(recdat.tools or {}) do
					local item = GM.items[k]
					
					if item then
						draw.RoundedBox(4,bx+4,cy+24+18+18*i,16,16,Color(0,0,0,100))
						surface.SetTexture(item:onGetIconTexID())
						surface.SetDrawColor(255,255,255,255)
						surface.DrawTexturedRect(bx+4,cy+24+18+18*i,16,16)
						
						local col = Color(255,255,255,255)
						if not (GM.inv[k] and GM.inv[k].quantity >= v) then
							col = Color(255,0,0,255)
						end
						
						draw.DrawText(v.."x "..item.label,"Trebuchet18",bx+16+4+4,cy+24+18+18*i,col,0)
					else
						draw.DrawText("ERROR","Trebuchet18",bx+16+4+4,cy+24+18+18*i,Color(255,0,0,255),0)
					end
					i = i+1
				end
			end
		end
		
		//Scrollbar
		do
			draw.RoundedBox(4,ix+iw+4,iy,32-4,ih,Color(0,0,0,100))
			
			
			local inch = 1/rows
			local disp = maxrows*inch
			
			local dp = {
				x = ix+iw+4+4,
				y = iy+4,
				w = 32-4-8,
				h = (ih-8),
			}
			local d = {
				uid = 1,
				x = dp.x,
				y = dp.y+dat.scroll*inch*dp.h,
				w = dp.w,
				h = dp.h*disp,
			}
			local col = Color(0,0,0,100)
			
			if m.y >= dp.y and m.y <= dp.y+dp.h then
				if m.x >= d.x and m.x <= d.x+d.w then
					col = Color(0,0,255,100)
					if m.c[0] == 1 then
						m.c[0] = 2
						m.f = {"scroll",m.y-d.y}
					end
				end
				if m.c[0] == 2 and type(m.f) == "table" and m.f[1] == "scroll" then
					dat.scroll = math.Clamp(math.Round((m.y-m.f[2]-dp.y)/inch/dp.h),0,math.max(0,rows-maxrows))
					col = Color(255,0,0,100)
				end
			end
			
			draw.RoundedBox(4,d.x,d.y,d.w,d.h,col)
			
		end
		
		//Mouse reset
		for k,v in pairs(m.c) do
			if v == 1 or v == -1 then
				//if type(m.f) == "table" and m.f[1] == "invdrag" then
				//	GM.invdrag.item = nil
				//end
				m.c[k] = 0
				m.f = 0
			end
		end
	end,
	OnCursorMoved = function(self,x,y)
		self:GetParent():GetTable().m.x = x
		self:GetParent():GetTable().m.y = y
	end,
	OnCursorExited = function(self)
		self:GetParent():GetTable().m.x = 0
		self:GetParent():GetTable().m.y = 0
		self:GetParent():GetTable().m.c[0] = 0
		self:GetParent():GetTable().m.c[1] = 0
		self:GetParent():GetTable().m.f = 0
	end,
	OnMousePressed = function(self,mc)
		if mc >= 107 then
			mc = mc - 107
		end
		if mc == 0 or mc == 1 then
			self:GetParent():GetTable().m.c[mc] = 1
			self:GetParent():GetTable().m.f = 0
		end
	end,
	OnMouseReleased = function(self,mc)
		//local GM = gmod.GetGamemode()
		if mc >= 107 then
			mc = mc - 107
		end
		if mc == 0 or mc == 1 then
			self:GetParent():GetTable().m.c[mc] = -1
			//GM.invdrag.m.c[mc] = -1
		end
	end,
	OnMouseWheeled = function(self,delta)
		local GM = gmod.GetGamemode()
		if GM.invdrag and GM.invdrag.item then
			delta = math.Round(math.Clamp(delta,-1,1))
			
			GM.invdrag.quantity = GM.invdrag.quantity or 1
			local invdat = GM.inv[GM.invdrag.item.class]
			local max = 1
			if invdat and GM.invdrag.itemfrom == -1 then
				max = invdat.quantity
			elseif GM.invdrag.itemfrom then
				local tab = GM.buildingdata[GM.invdrag.itemfrom]
				if tab then
					tab = tab.inventory[GM.invdrag.item.class]
					if tab and tab.quantity then
						max = tonumber(tab.quantity) or 1
					end
				end
			end
			GM.invdrag.quantity = math.Clamp(GM.invdrag.quantity + delta,1,max)
			
			return
		end
		local dat = self:GetParent():GetTable()
		local iw = self:GetWide()-32
		local ih = self:GetTall()-128-20
		local xm = math.floor(iw/(64+4))
		local ym = math.floor(ih/(64+4))
		local elements = #GM.inv
		local rows = math.max(math.ceil(elements/xm),ym)
		if dat.tab == 2 then
			ym = math.floor((ih-8)/28)
			rows = math.max(table.Count(GM.recipes))
		end
		
		if delta > 0 then
			dat.scroll = math.Clamp(dat.scroll - 1,0,math.max(0,rows-ym))
		else
			dat.scroll = math.Clamp(dat.scroll + 1,0,math.max(0,rows-ym))
		end
	end,
	OnCheckScroll = function(self)
		local dat = self:GetParent():GetTable()
		local iw = self:GetWide()-32
		local ih = self:GetTall()-128-20
		local xm = math.floor(iw/(64+4))
		local ym = math.floor(ih/(64+4))
		local elements = #GM.inv
		local rows = math.max(math.ceil(elements/xm),ym)
		if dat.tab == 2 then
			ym = math.floor((ih-8)/28)
			rows = math.max(table.Count(GM.recipes))
		end
		
		dat.scroll = math.Clamp(dat.scroll,0,math.max(0,rows-ym))
	end,
}

vgui.Register("gmInventory",inv.menuInvPanel)

inv.closeInventory = function()
	if type(inv.menuInventory) == "table" then
		inv.menuInventory.main:Remove()
		inv.menuInventory = nil
		GM.slotinvquickclose()
	end
end

hook.Add("Think","wscloseInventory",function()
	if type(inv.menuInventory) == "table" and not inv.menuInventory.main:IsVisible() then
		inv.closeInventory()
	end
end)

do
	local inventory = GM.items //INVENTORYDIR
	GM.invdrag = {
		item = nil,
		m = {
			c = {
				[0] = 0,
				[1] = 0,
			},
			f = 0,
		},
		hoverslot = nil,
	}
end

hook.Add("PostRenderVGUI","wsinvdrag",function()
	local self = gmod.GetGamemode()
	local inventory = GM.items //INVENTORYDIR
	local invdrag = self.invdrag
	local m = invdrag.m
	
	if not invdrag.item then
		self.invdrag.quantity = nil
		return
	end
	
	m.x,m.y = gui.MousePos()
	local overloot = self.lootventory.isMouseOver()
	
	local drop = false
	if self.inventory.menuInventory and self.inventory.menuInventory.main:IsVisible() then
		local dx,dy = self.inventory.menuInventory.main:GetPos()
		local dw,dh = self.inventory.menuInventory.main:GetSize()
		
		if m.x > dx+dw and m.y < dy+dh then
			drop = true
		end
	end
	
	local remove = false
	//Check release
	if not input.IsMouseDown(107) then
		remove = true
		if invdrag.hovercombine then
			//Add item to combine slot
			self.ingredients[invdrag.hovercombine] = invdrag.item.class
		elseif invdrag.hoverslot and not overslot then
			//Add item to slot inventory
			self.invsel.items[invdrag.hoverslot] = invdrag.item
		elseif drop and not overloot then
			//Drop the item
			if self.invdrag.itemfrom == -1 then
				RunConsoleCommand("wsdrop",invdrag.item.class,invdrag.quantity or 1)
			else
				RunConsoleCommand("wsdrop",invdrag.item.class,invdrag.quantity or 1,self.invdrag.itemfrom)
			end
		else
			if invdrag.itemfrom and invdrag.itemto and invdrag.itemfrom ~= invdrag.itemto then
				RunConsoleCommand("wsmove",invdrag.item.class,invdrag.itemfrom,invdrag.itemto,invdrag.quantity or 1)
			end
		end
	end
	
	//Draw dragged
	invdrag.item:onInvPaint(m.x,m.y,64,64,nil,{noquantity = true})
	if drop and not overloot then
		local msg = "Drop"
		if invdrag and invdrag.quantity and tonumber(invdrag.quantity) > 1 then
			msg = msg.." "..tostring(invdrag.quantity)
		end
		draw.DrawText(msg,"Trebuchet24",m.x,m.y-24,Color(255,255,255,255),1)
	else
		if invdrag and invdrag.quantity and tonumber(invdrag.quantity) > 1 then
			draw.DrawText(tostring(invdrag.quantity),"Trebuchet24",m.x,m.y-24,Color(255,255,255,255),1)
		end
		//draw.DrawText("Move to "..tostring(invdrag.itemtolabel).." from "..tostring(invdrag.itemfromlabel),"Trebuchet24",m.x,m.y-24,Color(255,255,255,255),1)
	end
	
	//Mouse reset
	for k,v in pairs(m.c) do
		if v == 1 or v == -1 then
			m.c[k] = 0
			m.f = 0
		end
	end
	
	if remove then
		invdrag.item = nil
	end
end)

hook.Add("GUIMousePressed","wsinvdrag",function(mc)
	local self = gmod.GetGamemode()
	if mc >= 107 then
		mc = mc - 107
	end
	
	if mc >= 0 and mc <= 1 then
		local m = self.invdrag.m
		m.c[mc] = 1
		m.f = 0
	end
end)

hook.Add("GUIMouseReleased","wsinvdrag",function(mc)
	local self = gmod.GetGamemode()
	if mc >= 107 then
		mc = mc - 107
	end
	
	if mc >= 0 and mc <= 1 then
		local m = self.invdrag.m
		m.c[mc] = -1
	end
end)














































































hook.Add("PlayerBindPress","gamemode",function(pl,bind,down)
	local self = gmod.GetGamemode()
	local inv = self.inventory
	if bind == "+menu_context" then
		if down then
			if not inv.openTime then
				inv.openInventory()
				inv.openTime = CurTime()
			end
		else
			if inv.openTime then
				if CurTime() - inv.openTime >= .2 then
					inv.closeInventory()
				end
					inv.openTime = nil
			end
		end
	elseif bind == "+menu" then
		if down then
		else
		end
	end
end)









































GM.whitegui = [[
"hud.res"
{
	"whitegui"
	{
		"ControlName"		"Frame"
		"fieldName"		"eInv"
		"xpos"		"475"
		"ypos"		"355"
		"zpos"		"290"
		"wide"		"400"
		"tall"		"66"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"0"
		"settitlebarvisible"		"1"
		"title"		"%TITLE%"
		"sizable"		"1"
	}
	"frame_topGrip"
	{
		"ControlName"		"Panel"
		"fieldName"		"frame_topGrip"
		"xpos"		"8"
		"ypos"		"0"
		"wide"		"384"
		"tall"		"5"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"frame_bottomGrip"
	{
		"ControlName"		"Panel"
		"fieldName"		"frame_bottomGrip"
		"xpos"		"8"
		"ypos"		"61"
		"wide"		"374"
		"tall"		"5"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"frame_leftGrip"
	{
		"ControlName"		"Panel"
		"fieldName"		"frame_leftGrip"
		"xpos"		"0"
		"ypos"		"0"
		"wide"		"5"
		"tall"		"66"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"frame_rightGrip"
	{
		"ControlName"		"Panel"
		"fieldName"		"frame_rightGrip"
		"xpos"		"395"
		"ypos"		"0"
		"wide"		"5"
		"tall"		"66"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"frame_tlGrip"
	{
		"ControlName"		"Panel"
		"fieldName"		"frame_tlGrip"
		"xpos"		"0"
		"ypos"		"0"
		"wide"		"8"
		"tall"		"8"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"frame_trGrip"
	{
		"ControlName"		"Panel"
		"fieldName"		"frame_trGrip"
		"xpos"		"392"
		"ypos"		"0"
		"wide"		"8"
		"tall"		"8"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"frame_blGrip"
	{
		"ControlName"		"Panel"
		"fieldName"		"frame_blGrip"
		"xpos"		"0"
		"ypos"		"58"
		"wide"		"8"
		"tall"		"8"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"frame_brGrip"
	{
		"ControlName"		"Panel"
		"fieldName"		"frame_brGrip"
		"xpos"		"382"
		"ypos"		"48"
		"wide"		"18"
		"tall"		"18"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"0"
		"enabled"		"1"
		"tabPosition"		"0"
	}
	"frame_caption"
	{
		"ControlName"		"Panel"
		"fieldName"		"frame_caption"
		"xpos"		"0"
		"ypos"		"0"
		"wide"		"390"
		"tall"		"23"
		"autoResize"		"0"
		"pinCorner"		"0"
		"visible"		"1"
		"enabled"		"1"
		"tabPosition"		"0"
	}
}
]]

GM.genwhitegui = function(caption)
	local self = gmod.GetGamemode()
	local frame = vgui.Create("Frame")
	frame:SetName("whitegui")
	frame:LoadControlsFromString(string.gsub(self.whitegui,"%%TITLE%%",caption))
	frame:SetName("caption")
	return frame
end